﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Discovery;
using XrmLibrary.Exceptions;

namespace XrmLibrary.Extensions
{
    public static class DiscoveryServiceExtensions
    {
        /// <summary>
        /// Retrieves the friendly name of the organization using specified organization unique name.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="uniqueName">Name of the unique.</param>
        /// <returns>Friendly name of organization.</returns>
        public static String RetrieveOrganizationFriendlyName(this IDiscoveryService service, String uniqueName)
        {
            return service.RetrieveOrganizationDetail(uniqueName).FriendlyName;
        }

        /// <summary>
        /// Retrieves the friendly name of the organization using specified organization id (GUID).
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="organizationId">The organization id.</param>
        /// <returns>Friendly name of organization.</returns>
        public static String RetrieveOrganizationFriendlyName(this IDiscoveryService service, Guid organizationId)
        {
            return service.RetrieveOrganizationDetail(organizationId).FriendlyName;
        }

        /// <summary>
        /// Retrieves the unique name of the organization using a specified organization id (GUID).
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="organizationId">The organization id.</param>
        /// <returns>Unique name of organization.</returns>
        public static String RetrieveOrganizationUniqueName(this IDiscoveryService service, Guid organizationId)
        {
            return service.RetrieveOrganizationDetail(organizationId).UniqueName;
        }

        /// <summary>
        /// Retrieves details of the organization using a specified organization unique name.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="uniqueName">Name of the unique.</param>
        /// <returns>Details of the organization.</returns>
        public static OrganizationDetail RetrieveOrganizationDetail(this IDiscoveryService service, String uniqueName)
        {
            RetrieveOrganizationResponse response = service.RetrieveOrganization(uniqueName);
            return response.Detail;
        }

        /// <summary>
        /// Retrieves details of the organization using a specified organization id (GUID).
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="organizationId">The organization id.</param>
        /// <returns>Details of the organization.</returns>
        public static OrganizationDetail RetrieveOrganizationDetail(this IDiscoveryService service, Guid organizationId)
        {
            RetrieveOrganizationsResponse response = service.RetrieveOrganizations();
            foreach (var detail in response.Details)
            {
                if (detail.OrganizationId == organizationId)
                {
                    return detail;
                }
            }
            throw new XrmLibraryException(String.Format("OrganizationId {0} does not exist.", organizationId));
        }

        /// <summary>
        /// Retrieves all organizations from the IDiscoveryService.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <returns>All organizations.</returns>
        public static RetrieveOrganizationsResponse RetrieveOrganizations(this IDiscoveryService service)
        {
            RetrieveOrganizationsRequest request = new RetrieveOrganizationsRequest();
            return (RetrieveOrganizationsResponse)service.Execute(request);
        }

        /// <summary>
        /// Retrieves an organization for a specified unique organization name.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="uniqueName">Name of the unique.</param>
        /// <returns>The organization.</returns>
        public static RetrieveOrganizationResponse RetrieveOrganization(this IDiscoveryService service, String uniqueName)
        {
            RetrieveOrganizationRequest request = new RetrieveOrganizationRequest { UniqueName = uniqueName };
            return (RetrieveOrganizationResponse)service.Execute(request);
        }
    }
}
